const MyPromise = require('./MyPromise')

const promise = new MyPromise((resolve, reject) => {
    // resolve('成功')
    // setTimeout(()=> resolve('异步成功'),1000)
    /**捕获报错*/
    // throw new Error('exector err')
    reject('失败')
})
const p2 = () => {
    return new MyPromise(resolve => {
        setTimeout(() => resolve('p2：异步成功'), 500)
    })
}
const p3 = () => {
    return new MyPromise((resolve, reject) => {
        setTimeout(() => reject('p3：异步失败'), 500)
    })
}
/**多次调用then方法*/
// promise.then(value=>{
//     console.log(value,1)
// }, reason => console.log(reason))

// promise.then(value=>{
//     console.log(value,2)
// }, reason => console.log(reason))

/**.then方法不能返回当前promise对象自身*/
// let p1 = promise.then(value=>{
//     console.log(value,1)
//     return p1
// }, reason => {
//     console.log(reason+' p1')
// })
// p1.then(value =>console.log(value), reason => console.log(reason+' p2失败'))

/**对peding和rejected状态补充链式调用*/

// let p1 = promise.then(value=>{
//     console.log(value,1)
// }, reason => {
//     console.log(reason+' p1')
//     return p2()
// })
// .then(value =>console.log(value), reason => console.log(reason+' p2失败'))

/**捕获报错*/
// let p1 = promise.then(value=>value, reason => {
//     throw new Error('then err')
// })
// .then(value =>console.log(value), reason => console.log(reason+' p2失败'))


/**then参数可选*/
// promise.then().then().then(value=> console.log(value), reason => console.log(reason))

/**Promise.all()*/

// MyPromise.all(['2', 5, p2(), p3(), promise]).then(value => console.log(value), reason => console.log(reason+' fail'))

/**Promise.resolve()*/
// MyPromise.resolve(p2()).then(value => console.log(value))

/**Promise.finally()*/
// promise.finally(()=> {
//     console.log('finally')
//     return p2()
// })
// .then(value => console.log(value+ ' resolve'), reason => console.log(reason+' reject'))

/**Promise.catch()*/
// p3()
// .then(value => console.log(value))
// .catch(reason => console.log(reason+' catch'))
